package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.bo.ExerciseBo;
import com.ruoyi.business.domain.vo.ExerciseVo;
import com.ruoyi.business.mapper.PlainMapper;
import com.ruoyi.business.mapper.UserMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.ExerciseMapper;
import com.ruoyi.business.domain.Exercise;
import com.ruoyi.business.service.IExerciseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 锻炼Service业务层处理
 * 
 * @author 吴倩芸
 * @date 2023-03-09
 */
@SuppressWarnings("ALL")
@Service
public class ExerciseServiceImpl extends ServiceImpl<ExerciseMapper, Exercise> implements IExerciseService
{
    @Autowired
    private ExerciseMapper exerciseMapper;

    @Autowired
    private PlainMapper plainMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询锻炼
     * 
     * @param id 锻炼主键
     * @return 锻炼
     */
    @Override
    public Exercise selectExerciseById(Long id)
    {
        return exerciseMapper.selectExerciseById(id);
    }

    /**
     * 查询锻炼列表
     * 
     * @param exercise 锻炼
     * @return 锻炼
     */
    @Override
    public List<ExerciseVo> selectExerciseList(ExerciseBo bo)
    {
        List<Exercise> exercises = exerciseMapper.selectExerciseList(bo);
        return exercises.stream().map(item -> {
            return toExerciseVo(item);
        }).collect(Collectors.toList());
    }

    public ExerciseVo toExerciseVo(Exercise exercise) {
        ExerciseVo vo = new ExerciseVo();
        BeanUtils.copyProperties(exercise, vo);
        if (null != exercise.getPlainId()) {
            vo.setPlain(plainMapper.selectPlainById(exercise.getPlainId()));
        }
        if (null != exercise.getUserId()) {
            vo.setUser(userMapper.selectUserById(exercise.getUserId()));
        }
        return vo;
    }

    /**
     * 新增锻炼
     * 
     * @param exercise 锻炼
     * @return 结果
     */
    @Override
    public int insertExercise(Exercise exercise)
    {
        if (exercise.getUserId() == null) {
            exercise.setUserId(SecurityUtils.getUserId());
        }
        return exerciseMapper.insertExercise(exercise);
    }

    /**
     * 修改锻炼
     * 
     * @param exercise 锻炼
     * @return 结果
     */
    @Override
    public int updateExercise(Exercise exercise)
    {
        return exerciseMapper.updateExercise(exercise);
    }

    /**
     * 批量删除锻炼
     * 
     * @param ids 需要删除的锻炼主键
     * @return 结果
     */
    @Override
    public int deleteExerciseByIds(Long[] ids)
    {
        return exerciseMapper.deleteExerciseByIds(ids);
    }

    /**
     * 删除锻炼信息
     * 
     * @param id 锻炼主键
     * @return 结果
     */
    @Override
    public int deleteExerciseById(Long id)
    {
        return exerciseMapper.deleteExerciseById(id);
    }

    @Override
    public int signOut(ExerciseBo bo) {
        if (bo.getUserId() == null) {
            bo.setUserId(SecurityUtils.getUserId());
        }
        List<Exercise> exercises = exerciseMapper.selectExerciseList(bo);
        if (exercises != null && exercises.size() == 1) {
            Exercise exercise = exercises.get(0);
            exercise.setEndTime(new Date());
            return updateExercise(exercise);
        }else {
            throw new RuntimeException("系统错误，请联系管理员");
        }
    }
}
